package cn.iocoder.yudao.module.pi.service.deploy;

import cn.iocoder.yudao.framework.common.pojo.PageResult;
import cn.iocoder.yudao.framework.common.util.object.BeanUtils;
import cn.iocoder.yudao.module.pi.controller.admin.pipline.deploy.vo.DeployPageReqVO;
import cn.iocoder.yudao.module.pi.controller.admin.pipline.deploy.vo.DeploySaveReqVO;
import cn.iocoder.yudao.module.pi.dal.dataobject.deploy.DeployDO;
import cn.iocoder.yudao.module.pi.dal.mysql.deploy.DeployMapper;
import cn.iocoder.yudao.module.pi.enums.ErrorCodeConstants;
import org.springframework.stereotype.Service;
import org.springframework.validation.annotation.Validated;

import javax.annotation.Resource;

import static cn.iocoder.yudao.framework.common.exception.util.ServiceExceptionUtil.exception;


/**
 * 部署 Service 实现类
 *
 * @author 芋道源码
 */
@Service
@Validated
public class DeployServiceImpl implements DeployService {

    @Resource
    private DeployMapper deployMapper;

    @Override
    public Long createDeploy(DeploySaveReqVO createReqVO) {
        // 插入
        DeployDO deploy = BeanUtils.toBean(createReqVO, DeployDO.class);
        deployMapper.insert(deploy);
        // 返回
        return deploy.getId();
    }

    @Override
    public void updateDeploy(DeploySaveReqVO updateReqVO) {
        // 校验存在
        validateDeployExists(updateReqVO.getId());
        // 更新
        DeployDO updateObj = BeanUtils.toBean(updateReqVO, DeployDO.class);
        deployMapper.updateById(updateObj);
    }

    @Override
    public void deleteDeploy(Long id) {
        // 校验存在
        validateDeployExists(id);
        // 删除
        deployMapper.deleteById(id);
    }

    private void validateDeployExists(Long id) {
        if (deployMapper.selectById(id) == null) {
            throw exception(ErrorCodeConstants.DEPLOY_NOT_EXISTS);
        }
    }

    @Override
    public DeployDO getDeploy(Long id) {
        return deployMapper.selectById(id);
    }

    @Override
    public PageResult<DeployDO> getDeployPage(DeployPageReqVO pageReqVO) {
        return deployMapper.selectPage(pageReqVO);
    }

}